rm(list = ls())
library(data.table)
library(plyr)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)
library(sandwich)
library(roll)
library(readxl)
library(readr)
library(zoo)
library(texreg)
library(DescTools)

m <- data.table(read_xlsx("../Data/MasterData.xlsx", skip = 1, guess_max = 1e4))
m[, age := 2021 - year]

m[, memrableperiod_ownret := memrableperiod_ownret * 100]
m[, memrableperiod_pret := memrableperiod_pret * 100]
m[, memrableperiod_aret := memrableperiod_aret * 100]
m[, bias := (memrableperiod_pret - memrableperiod_aret)]

m[, retrecall_1day_rate := retrecall_1day_rate * 100]
m[, retrecall_30day_rate := retrecall_30day_rate * 100]
m[, retrecall_1year_rate := retrecall_1year_rate * 100]
m[, retrecall_5year_rate := retrecall_5year_rate * 100]

m[, expret30day_market_rate := expret30day_market_rate * 100]
m[, expret1year_market_rate := expret1year_market_rate * 100]
m[, expret30day_self_rate := expret30day_self_rate * 100]
m[, expret1year_self_rate := expret1year_self_rate * 100]

m[, memrableperiod_begin := as.yearmon(memrableperiod_begin, "%Ym%m")]
m[, memrableperiod_end := as.yearmon(memrableperiod_end, "%Ym%m")]
m[, memrableperiod_dist := as.yearmon(2022 + 2 / 12) - (memrableperiod_begin + memrableperiod_end) / 2]

C <- data.table(read_xlsx("../Data/AccountData.xlsx", guess_max = 1e4))

C[, holdchg_tday_21 := (netbuy_tday_21 / asize_tday_21) * 100]
C[is.infinite(holdchg_tday_21), holdchg_tday_21 := NA]

C[, holdchg_tday1 := as.numeric(holdchg_tday1) * 100]
C[, holdchg_tday5 := as.numeric(holdchg_tday5) * 100]
C[, asize_tday1 := as.numeric(asize_tday1)]
C[, asize_tday_1 := as.numeric(asize_tday_1)]
C[, asize_tday_21 := as.numeric(asize_tday_21) * 100]
C[, beta_month_hold_tsa := as.numeric(beta_month_hold_tsa)]
C[, netbuy_tday1 := as.numeric(netbuy_tday1)]
C[, netbuy_tday5 := as.numeric(netbuy_tday5)]
C[, netbuy_tday21 := as.numeric(netbuy_tday21)]
C[, netbuy_tday_1 := as.numeric(netbuy_tday_1)]
C[, netbuy_tday_5 := as.numeric(netbuy_tday_5)]
C[, memrableperiod_aret2 := as.numeric(memrableperiod_aret2)]
C[, aret_1day_v2 := as.numeric(aret_1day_v2) * 100]
C[, aret_4_1week_v2 := as.numeric(aret_4_1week_v2) * 100]
C[, max_invest_tday_1 := as.numeric(max_invest_tday_1)]
C[, max_invest_5year := as.numeric(max_invest_5year)]

m <- merge(m, C[, .(id, holdchg_tday1, holdchg_tday5, holdchg_tday_21,
    asize_tday1, asize_tday_21, asize_tday_1,
    netbuy_tday1, netbuy_tday5, netbuy_tday21,
    netbuy_tday_1, netbuy_tday_5,
    memrableperiod_aret2, 
    aret_1day_v2, aret_4_1week_v2,
    beta_month_hold_tsa, max_invest_tday_1, max_invest_5year)],
    by = "id", all.x = T
)

m[, holdchg_tday1 := Winsorize(holdchg_tday1, quantile(holdchg_tday1, probs = c(0.05, 0.95), na.rm = T))]
m[, holdchg_tday5 := Winsorize(holdchg_tday5, quantile(holdchg_tday5, probs = c(0.05, 0.95), na.rm = T))]
m[, holdchg_tday_21 := Winsorize(holdchg_tday_21, quantile(holdchg_tday_21, probs = c(0.05, 0.95), na.rm = T))]
m[, netbuy_tday1 := Winsorize(netbuy_tday1, quantile(netbuy_tday1, probs = c(0.05, 0.95), na.rm = T))]
m[, netbuy_tday5 := Winsorize(netbuy_tday5, quantile(netbuy_tday5, probs = c(0.05, 0.95), na.rm = T))]
m[, netbuy_tday21 := Winsorize(netbuy_tday21, quantile(netbuy_tday21, probs = c(0.05, 0.95), na.rm = T))]
m[, netbuy_tday_1 := Winsorize(netbuy_tday_1, quantile(netbuy_tday_1, probs = c(0.05, 0.95), na.rm = T))]
m[, netbuy_tday_5 := Winsorize(netbuy_tday_5, quantile(netbuy_tday_5, probs = c(0.05, 0.95), na.rm = T))]

m[, expret30day_market_rate := Winsorize(expret30day_market_rate, quantile(expret30day_market_rate, probs = c(0.05, 0.95), na.rm = T))]
m[, expret30day_self_rate := Winsorize(expret30day_self_rate, quantile(expret30day_self_rate, probs = c(0.05, 0.95), na.rm = T))]
m[, expret1year_self_rate := Winsorize(expret1year_self_rate, quantile(expret1year_self_rate, probs = c(0.05, 0.95), na.rm = T))]
m[, retrecall_30day_rate := Winsorize(retrecall_30day_rate, quantile(retrecall_30day_rate, probs = c(0.05, 0.95), na.rm = T))]
m[, retrecall_1day_rate := Winsorize(retrecall_1day_rate, quantile(retrecall_1day_rate, probs = c(0.05, 0.95), na.rm = T))]
m[, asize_tday_1 := Winsorize(asize_tday_1, quantile(asize_tday_1, probs = c(0.05, 0.95), na.rm = T))]
m[, aret_1day_v2 := Winsorize(aret_1day_v2, quantile(aret_1day_v2, probs = c(0.05, 0.95), na.rm = T))]
m[, aret_4_1week_v2 := Winsorize(aret_4_1week_v2, quantile(aret_4_1week_v2, probs = c(0.05, 0.95), na.rm = T))]
m[, memrableperiod_aret2 := Winsorize(memrableperiod_aret2, quantile(memrableperiod_aret2, probs = c(0.05, 0.95), na.rm = T))]

###########################################################################
# Table 11
sample <- m[!is.na(netbuy_tday21) & !is.na(netbuy_tday1) &
    !is.na(retrecall_30day_rate) & !is.na(expret30day_self_rate)]

f1 <- felm(netbuy_tday1 ~  expret30day_self_rate + expret30day_market_rate  | age + gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

f2 <- felm(netbuy_tday21 ~ expret30day_self_rate + expret30day_market_rate | age + gender + education + total_wealth + total_income +
             accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

sample <- m[!is.na(netbuy_tday21) & !is.na(netbuy_tday1) &
    !is.na(retrecall_1year_rate) & !is.na(retrecall_30day_rate) & !is.na(expret30day_self_rate)]

fit_model <- felm(expret30day_self_rate ~ retrecall_30day_rate + retrecall_1year_rate | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

expret30day_self_rate_fitted <- fitted(fit_model)
sample$expret30day_self_rate_fitted <- expret30day_self_rate_fitted

f3 <- felm(netbuy_tday1 ~ expret30day_self_rate_fitted | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

f4 <- felm(netbuy_tday21 ~ expret30day_self_rate_fitted | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

f5 <- felm(netbuy_tday1 ~ expret30day_self_rate_fitted + aret_4_1week_v2 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

f6 <- felm(netbuy_tday21 ~ expret30day_self_rate_fitted + aret_4_1week_v2 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

stargazer(f1, f3, f5,
          align = TRUE, dep.var.labels.include = TRUE,
          covariate.labels = c("Expected own return, 1M", "Expected market return, 1M", 
                               "Expected own return, 1M", "Actual own return, 1M"),
          omit.stat = c("LL", "ser", "F", "rsq"), ord.intercepts = FALSE, no.space = TRUE,
          single.row = FALSE, column.sep.width = "0pt", digits = 2
)

stargazer(f2, f4, f6,
          align = TRUE, dep.var.labels.include = TRUE,
          covariate.labels = c("Expected own return, 1M", "Expected market return, 1M", 
                               "Expected own return, 1M", "Actual own return, 1M"),
          omit.stat = c("LL", "ser", "F", "rsq"), ord.intercepts = FALSE, no.space = TRUE,
          single.row = FALSE, column.sep.width = "0pt", digits = 2
)

###########################################################################
# Table A.14: Past actions and recall

m[, netbuy_tday_1 := netbuy_tday_1 / 1e3]
m[, netbuy_tday_5 := netbuy_tday_5 / 1e3]
# Panel A
sample <- m[!is.na(netbuy_tday_1) & !is.na(netbuy_tday_5) &
    !is.na(memrableperiod_pret) & !is.na(memrableperiod_aret)]


f1 <- felm(memrableperiod_pret ~ netbuy_tday_1 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f2 <- felm(memrableperiod_pret ~ netbuy_tday_1 + memrableperiod_aret | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f3 <- felm(memrableperiod_pret ~ netbuy_tday_5 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f4 <- felm(memrableperiod_pret ~ netbuy_tday_5 + memrableperiod_aret | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

sample <- m[!is.na(netbuy_tday_1) & !is.na(netbuy_tday_5) &
    !is.na(memrableperiod_ownret) & !is.na(memrableperiod_aret2)]

f5 <- felm(memrableperiod_ownret ~ netbuy_tday_1 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f6 <- felm(memrableperiod_ownret ~ netbuy_tday_1 + memrableperiod_aret2 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f7 <- felm(memrableperiod_ownret ~ netbuy_tday_5 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f8 <- felm(memrableperiod_ownret ~ netbuy_tday_5 + memrableperiod_aret2 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

stargazer(f1, f2, f3, f4, f5, f6, f7, f8,
    align = TRUE, dep.var.labels.include = TRUE,
    covariate.labels = c(
        "NetBuy\\textsubscript{yesterday}", "MktRet\\textsubscript{episode}",
        "NetBuy\\textsubscript{previous week}", "OwnRet\\textsubscript{episode}"
    ),
    omit.stat = c("LL", "ser", "F", "rsq"), ord.intercepts = FALSE, no.space = TRUE,
    single.row = FALSE, column.sep.width = "0pt", digits = 2
)

# Panel B
sample <- m[!is.na(netbuy_tday_1) & !is.na(netbuy_tday_5) &
    !is.na(retrecall_1day_rate) & !is.na(aret_1day_v2)]

f1 <- felm(retrecall_1day_rate ~ netbuy_tday_1 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f2 <- felm(retrecall_1day_rate ~ netbuy_tday_1 + aret_1day_v2 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f3 <- felm(retrecall_1day_rate ~ netbuy_tday_5 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f4 <- felm(retrecall_1day_rate ~ netbuy_tday_5 + aret_1day_v2 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

sample <- m[!is.na(netbuy_tday_1) & !is.na(netbuy_tday_5) &
    !is.na(retrecall_30day_rate) & !is.na(aret_4_1week_v2)]

f5 <- felm(retrecall_30day_rate ~ netbuy_tday_1 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f6 <- felm(retrecall_30day_rate ~ netbuy_tday_1 + aret_4_1week_v2 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f7 <- felm(retrecall_30day_rate ~ netbuy_tday_5 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)
f8 <- felm(retrecall_30day_rate ~ netbuy_tday_5 + aret_4_1week_v2 | age + gender + education + total_wealth + total_income +
    accountcheck_freq + newscheck_freq + discussion_freq + num_wechat | 0 | date, sample)

stargazer(f1, f2, f3, f4, f5, f6, f7, f8,
    align = TRUE, dep.var.labels.include = TRUE,
    covariate.labels = c(
        "NetBuy\\textsubscript{yesterday}", "OwnRet\\textsubscript{1D}",
        "NetBuy\\textsubscript{previous week}", "OwnRet\\textsubscript{1M}"
    ),
    omit.stat = c("LL", "ser", "F", "rsq"), ord.intercepts = FALSE, no.space = TRUE,
    single.row = FALSE, column.sep.width = "0pt", digits = 2
)
